<?php
/**
 * bll savefile
 * @package app-file_lib_bll
 */
load_lib('/bll/file');
/**
 * bll savefile
 * @author pandajingjing
 * @package app-file_lib_bll
 */
class bll_savefile extends bll_file{

	function __construct(){
		parent::__construct();
	}

	/**
	 * 保存上传文件
	 * @param string $p_strName
	 * @param string $p_strMimeType
	 * @param string $p_strTmpName
	 * @param int $p_intError
	 * @param int $p_intSize
	 * @param string $p_strIP
	 * @param array $o_arrFileInfo
	 * @return true/string
	 */
	function saveFile($p_strName,$p_strMimeType,$p_strTmpName,$p_intError,$p_intSize,$p_strIP,&$o_arrFileInfo){
		if($p_intError>0){
			return 'Sys_Error_Upload';
		}
		if(is_uploaded_file($p_strTmpName)){
			load_lib('/dao/filedao');
			$arrAllowedSize=dao_filedao::getConfig('upload_size','upload');
			$bolGetIt=false;
			foreach($arrAllowedSize as $strPattern=>$arrSize){
				if(1===preg_match($strPattern,$this->_strDomain)){
					$bolGetIt=true;
					break;
				}
			}
			if($bolGetIt){
				$arrAllowedSize=$arrSize;
			}else{
				throw new Exception(__CLASS__.': configuration(upload_size) lost.');
				return false;
			}
			if($p_intSize<$arrAllowedSize['min']){
				return 'Err_Logic_Size_TooSmall';
			}elseif($p_intSize>$arrAllowedSize['max']){
				return 'Err_Logic_Size_TooLarge';
			}
			load_lib('/util/file');
			if('application/octet-stream'==$p_strMimeType){
				$objFInfo=finfo_open();
				$p_strMimeType=finfo_file($objFInfo,$p_strTmpName,FILEINFO_MIME_TYPE);
				finfo_close($objFInfo);
			}
			$strExtension=util_file::getExtension($p_strMimeType);
			if('dat'==$strExtension){
				$this->addLog(array( 
						'Filename'=>$p_strName,
						'Mimetype'=>$p_strMimeType 
				),'unknow_mime');
			}
			$arrAllowedExtension=dao_filedao::getConfig('upload_type','upload');
			$bolGetIt=false;
			foreach($arrAllowedExtension as $strPattern=>$arrExtension){
				if(1===preg_match($strPattern,$this->_strDomain)){
					$bolGetIt=true;
					break;
				}
			}
			if($bolGetIt){
				$arrAllowedExtension=$arrExtension;
			}else{
				throw new Exception(__CLASS__.': configuration(upload_type) lost.');
				return false;
			}
			if(!in_array($strExtension,$arrAllowedExtension)){
				return 'Err_Logic_Ext_NotMatch';
			}
			$strFileKey=md5_file($p_strTmpName);
			$arrFileInfo=dao_filedao::getFileDetail($strFileKey);
			$arrImageType=dao_filedao::getConfig('image_type');
			if(in_array($strExtension,$arrImageType)){
				$bolImage=true;
			}else{
				$bolImage=false;
			}
			if(NULL===$arrFileInfo){
				$intHostID=$this->dispatchUploadHostID();
				$strSubDir=$this->dispatchFile($strFileKey);
				$strBaseDir=dao_filedao::getConfig('raw','storage');
				$strDir=$strBaseDir.DIRECTORY_SEPARATOR.$intHostID.$strSubDir;
				if(!is_dir($strDir)){
					if(false===util_file::tryMakeDir($strDir,0755,true)){
						throw new Exception(__CLASS__.': can not create path('.$strDir.').');
						return false;
					}
				}
				$strDestFile=$strDir.$strFileKey;
				$arrImageInfo=array();
				if($bolImage){
					$arrImageInfo=$this->getImageInfo($strFileKey,$p_strTmpName);
					$o_arrFileInfo=array( 
							'FILEKEY'=>$strFileKey,
							'EXTENSION'=>$strExtension,
							'FILESIZE'=>$p_intSize,
							'WIDTH'=>$arrImageInfo['WIDTH'],
							'HEIGHT'=>$arrImageInfo['HEIGHT'] 
					);
				}else{
					$o_arrFileInfo=array( 
							'FILEKEY'=>$strFileKey,
							'EXTENSION'=>$strExtension,
							'FILESIZE'=>$p_intSize 
					);
				}
				if(false===move_uploaded_file($p_strTmpName,$strDestFile)){
					throw new Exception(__CLASS__.': can not move upload file to file('.$strDestFile.').');
					return false;
				}else{
					$arrDFS=array( 
							'FILEKEY'=>$strFileKey,
							'MIMETYPE'=>$p_strMimeType,
							'HOSTID'=>$intHostID 
					);
					$arrDFSMsg=array_merge(array( 
							'MSGTYPE'=>1,
							'FILEKEY'=>$strFileKey,
							'FILESIZE'=>$p_intSize,
							'FILEEXT'=>$strExtension,
							'FROMDOMAIN'=>$this->_strDomain,
							'FROMIP'=>$p_strIP,
							'HOSTID'=>$intHostID 
					),$arrImageInfo);
					dao_filedao::saveFile($arrDFS);
					dao_filedao::addFileQueue($arrDFSMsg);
					return true;
				}
			}else{
				if($bolImage){
					$arrImageInfo=dao_filedao::getImageInfo($arrFileInfo['FILEKEY']);
					if(null===$arrImageInfo){
						$arrImageInfo=$this->getImageInfo($strFileKey,$p_strTmpName);
					}
					$o_arrFileInfo=array( 
							'FILEKEY'=>$strFileKey,
							'EXTENSION'=>$strExtension,
							'FILESIZE'=>$p_intSize,
							'WIDTH'=>$arrImageInfo['WIDTH'],
							'HEIGHT'=>$arrImageInfo['HEIGHT'] 
					);
				}else{
					$o_arrFileInfo=array( 
							'FILEKEY'=>$strFileKey,
							'EXTENSION'=>$strExtension,
							'FILESIZE'=>$p_intSize 
					);
				}
				return true;
			}
		}else{
			return 'Sys_Error_Upload';
		}
	}

	/**
	 * 给文件分配存储设备
	 * @return int
	 */
	protected function dispatchUploadHostID(){
		load_lib('/dao/filedao');
		$arrHostConfig=dao_filedao::getConfig('storage_host_id','upload');
		return $this->dispatchHostID($arrHostConfig);
	}

	/**
	 * 得到图片的扩展信息
	 * @param string $p_strFileKey
	 * @param string $p_strTmpName
	 * @return array
	 */
	protected function getImageInfo($p_strFileKey,$p_strTmpName){
		$arrImageInfo=getimagesize($p_strTmpName);
		return array( 
				'FILEKEY'=>$p_strFileKey,
				'WIDTH'=>$arrImageInfo[0],
				'HEIGHT'=>$arrImageInfo[1],
				'CHANNELS'=>isset($arrImageInfo['channels'])?$arrImageInfo['channels']:'0',
				'BITS'=>isset($arrImageInfo['bits'])?$arrImageInfo['bits']:'0' 
		);
	}
}